home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / Managed / Direct3D / PrtPerVertex / PrtOptions.cs < prev    next >
Encoding:
Text File  |  2004-09-27  |  19.6 KB  |  376 lines

  1. //--------------------------------------------------------------------------------------
  2. // File: PrtOptionsForm.cs
  3. //
  4. // Copyright (c) Microsoft Corporation. All rights reserved.
  5. //--------------------------------------------------------------------------------------
  6.  
  7. using System;
  8. using System.Collections;
  9. using System.Xml;
  10. using System.Configuration;
  11. using System.Drawing;
  12. using Microsoft.DirectX;
  13. using Microsoft.DirectX.Direct3D;
  14. using Microsoft.Samples.DirectX.UtilityToolkit;
  15.  
  16. namespace PrtPerVertexSample
  17. {
  18.     class PrtOptions
  19.     {
  20.         #region Public Static Member Variables
  21.  
  22.         #region Constants
  23.         public const int MaxPcaVectors = 24;
  24.         
  25.         /// <summary>
  26.         /// Subsurface scattering parameters from:
  27.         /// "A Practical Model for Subsurface Light Transport", 
  28.         /// Henrik Wann Jensen, Steve R. Marschner, Marc Levoy, Pat Hanrahan.
  29.         /// SIGGRAPH 2001
  30.         /// </summary>
  31.         public static PredefinedMaterial[] PredefinedMaterials = new PredefinedMaterial[]
  32.         {
  33.             //      name                         scattering (R/G/B/A)                          absorption (R/G/B/A)                                  reflectance (R/G/B/A)                         index of refraction
  34.             new PredefinedMaterial("Default",    new ColorValue(2.00f, 2.00f, 2.00f, 1.0f),    new ColorValue(0.0030f, 0.0030f, 0.0460f, 1.0f),      new ColorValue(1.00f, 1.00f, 1.00f, 1.0f),    1.3f),
  35.             new PredefinedMaterial("Apple",      new ColorValue(2.29f, 2.39f, 1.97f, 1.0f),    new ColorValue(0.0030f, 0.0030f, 0.0460f, 1.0f),      new ColorValue(0.85f, 0.84f, 0.53f, 1.0f),    1.3f),
  36.             new PredefinedMaterial("Chicken1",   new ColorValue(0.15f, 0.21f, 0.38f, 1.0f),    new ColorValue(0.0150f, 0.0770f, 0.1900f, 1.0f),      new ColorValue(0.31f, 0.15f, 0.10f, 1.0f),    1.3f),
  37.             new PredefinedMaterial("Chicken2",   new ColorValue(0.19f, 0.25f, 0.32f, 1.0f),    new ColorValue(0.0180f, 0.0880f, 0.2000f, 1.0f),      new ColorValue(0.32f, 0.16f, 0.10f, 1.0f),    1.3f),
  38.             new PredefinedMaterial("Cream",      new ColorValue(7.38f, 5.47f, 3.15f, 1.0f),    new ColorValue(0.0002f, 0.0028f, 0.0163f, 1.0f),      new ColorValue(0.98f, 0.90f, 0.73f, 1.0f),    1.3f),
  39.             new PredefinedMaterial("Ketchup",    new ColorValue(0.18f, 0.07f, 0.03f, 1.0f),    new ColorValue(0.0610f, 0.9700f, 1.4500f, 1.0f),      new ColorValue(0.16f, 0.01f, 0.00f, 1.0f),    1.3f),
  40.             new PredefinedMaterial("Marble",     new ColorValue(2.19f, 2.62f, 3.00f, 1.0f),    new ColorValue(0.0021f, 0.0041f, 0.0071f, 1.0f),      new ColorValue(0.83f, 0.79f, 0.75f, 1.0f),    1.5f),
  41.             new PredefinedMaterial("Potato",     new ColorValue(0.68f, 0.70f, 0.55f, 1.0f),    new ColorValue(0.0024f, 0.0090f, 0.1200f, 1.0f),      new ColorValue(0.77f, 0.62f, 0.21f, 1.0f),    1.3f),
  42.             new PredefinedMaterial("Skimmilk",   new ColorValue(0.70f, 1.22f, 1.90f, 1.0f),    new ColorValue(0.0014f, 0.0025f, 0.0142f, 1.0f),      new ColorValue(0.81f, 0.81f, 0.69f, 1.0f),    1.3f),
  43.             new PredefinedMaterial("Skin1",      new ColorValue(0.74f, 0.88f, 1.01f, 1.0f),    new ColorValue(0.0320f, 0.1700f, 0.4800f, 1.0f),      new ColorValue(0.44f, 0.22f, 0.13f, 1.0f),    1.3f),
  44.             new PredefinedMaterial("Skin2",      new ColorValue(1.09f, 1.59f, 1.79f, 1.0f),    new ColorValue(0.0130f, 0.0700f, 0.1450f, 1.0f),      new ColorValue(0.63f, 0.44f, 0.34f, 1.0f),    1.3f),
  45.             new PredefinedMaterial("Spectralon", new ColorValue(11.60f,20.40f,14.90f, 1.0f),   new ColorValue(0.0000f, 0.0000f, 0.0000f, 1.0f),      new ColorValue(1.00f, 1.00f, 1.00f, 1.0f),    1.3f),
  46.             new PredefinedMaterial("Wholemilk",  new ColorValue(2.55f, 3.21f, 3.77f, 1.0f),    new ColorValue(0.0011f, 0.0024f, 0.0140f, 1.0f),      new ColorValue(0.91f, 0.88f, 0.76f, 1.0f),    1.3f),
  47.             new PredefinedMaterial("Custom",     new ColorValue(0.00f, 0.00f, 0.00f, 1.0f),    new ColorValue(0.0000f, 0.0000f, 0.0000f, 1.0f),      new ColorValue(0.00f, 0.00f, 0.00f, 1.0f),    0.0f),
  48.         };
  49.  
  50.         public static Hashtable PredefinedMaterialsHT = initializeHashtable();
  51.  
  52.         #endregion
  53.  
  54.         #endregion
  55.  
  56.         #region Private Static Member Variables
  57.         private static OptionsFile optionsFile = new OptionsFile();
  58.         #endregion
  59.  
  60.         #region Public Static Member Functions
  61.         public static SimulatorOptions GlobalOptions { get { return optionsFile.Options; } }
  62.         public static OptionsFile GlobalOptionsFile { get { return optionsFile; } }
  63.         #endregion
  64.  
  65.         #region Private Static Member Functions
  66.         private static Hashtable initializeHashtable()
  67.         {
  68.             Hashtable ht = new Hashtable();
  69.             for(int i = 0; i < PredefinedMaterials.Length; i++)
  70.             {
  71.                 ht.Add(PredefinedMaterials[i].Name, PredefinedMaterials[i]);
  72.             }
  73.  
  74.             return ht;
  75.         }
  76.         #endregion
  77.     }
  78.  
  79.     /// <summary>
  80.     /// Struct to store material parameters
  81.     /// </summary>
  82.     public struct PredefinedMaterial
  83.     {
  84.         public string Name;
  85.         public ColorValue ReducedScattering;
  86.         public ColorValue Absorption;
  87.         public ColorValue Diffuse;
  88.         public float RelativeIndexOfRefraction;
  89.  
  90.         public PredefinedMaterial(string name, ColorValue reducedScattering, ColorValue absoption, ColorValue diffuse, float RelativeIndexOfRefraction)
  91.         {
  92.             this.Name = name;
  93.             this.ReducedScattering = reducedScattering;
  94.             this.Absorption = absoption;
  95.             this.Diffuse = diffuse;
  96.             this.RelativeIndexOfRefraction = RelativeIndexOfRefraction;
  97.         }
  98.     };
  99.  
  100.     class OptionsFile
  101.     {
  102.         #region Creation
  103.         public OptionsFile()
  104.         {
  105.             ResetSettings();
  106.  
  107.             // Find out the executing assembly information
  108.             System.Reflection.Assembly executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
  109.  
  110.             // And the executable's folder
  111.             string exeFolder = System.IO.Path.GetDirectoryName(executingAssembly.Location);
  112.  
  113.             fileName = exeFolder + "\\options.xml";
  114.             try
  115.             {
  116.                 LoadOptions(fileName);
  117.             }
  118.             catch(System.IO.FileNotFoundException)
  119.             {
  120.                 // Never mind. We will create the file again
  121.             }
  122.         }
  123.         #endregion
  124.  
  125.         #region Instance Data
  126.         private string fileName;
  127.         public SimulatorOptions options;
  128.         #endregion
  129.  
  130.         #region Properties
  131.         public SimulatorOptions Options { get{ return options;} }
  132.         #endregion
  133.  
  134.         public void LoadOptions()
  135.         {
  136.             LoadOptions(string.Empty);
  137.         }
  138.  
  139.         public void LoadOptions(string fileName)
  140.         {
  141.             if( fileName == string.Empty)
  142.                 fileName = this.fileName;
  143.  
  144.             XmlDocument doc = new XmlDocument();
  145.             XmlNode node;
  146.  
  147.             doc.Load(fileName);
  148.  
  149.             node = doc.FirstChild.FirstChild;
  150.  
  151.             options = new SimulatorOptions();
  152.             XmlHelper.GetValue( ref node, "InitialDir", out options.InitialDir );
  153.             XmlHelper.GetValue( ref node, "InputMesh", out options.InputMesh );
  154.             XmlHelper.GetValue( ref node, "ResultsFile", out options.ResultsFileName );
  155.             XmlHelper.GetValue( ref node, "Order", out options.Order );
  156.             XmlHelper.GetValue( ref node, "NumberRays", out options.NumberRays );
  157.             XmlHelper.GetValue( ref node, "NumberBounces", out options.NumberBounces );
  158.             XmlHelper.GetValue( ref node, "ShowTooltips", out options.ShowTooltips );
  159.  
  160.             XmlHelper.GetValue( ref node, "NumberClusters", out options.NumberClusters );
  161.             int quality;
  162.             XmlHelper.GetValue( ref node, "Quality", out quality );
  163.             options.Quality = (CompressionQuality) quality;
  164.             XmlHelper.GetValue( ref node, "NumberPcaVectors", out options.NumberPcaVectors );
  165.  
  166.             XmlHelper.GetValue( ref node, "IsSubsurfaceScattering", out options.IsSubsurfaceScattering );
  167.             XmlHelper.GetValue( ref node, "LengthScale", out options.LengthScale );
  168.             XmlHelper.GetValue( ref node, "NumberChannels", out options.NumberChannels );
  169.             XmlHelper.GetValue( ref node, "PredefinedMaterialIndex", out options.PredefinedMaterialIndex );
  170.  
  171.             XmlHelper.GetValue( ref node, "Diffuse", out options.Diffuse );
  172.             XmlHelper.GetValue( ref node, "Absorption", out options.Absorption );
  173.             XmlHelper.GetValue( ref node, "ReducedScattering", out options.ReducedScattering );
  174.  
  175.             XmlHelper.GetValue( ref node, "RelativeIndexOfRefraction", out options.RelativeIndexOfRefraction );
  176.  
  177.             XmlHelper.GetValue( ref node, "IsAdaptive", out options.IsAdaptive );
  178.             XmlHelper.GetValue( ref node, "IsRobustMeshRefine", out options.IsRobustMeshRefine );
  179.             XmlHelper.GetValue( ref node, "robustMeshRefineMinEdgeLength", out options.RobustMeshRefineMinEdgeLength );
  180.             XmlHelper.GetValue( ref node, "robustMeshRefineMaxSubdiv", out options.RobustMeshRefineMaxSubdiv );
  181.             XmlHelper.GetValue( ref node, "IsAdaptiveDL", out options.IsAdaptiveDL );
  182.             XmlHelper.GetValue( ref node, "AdaptiveDLMinEdgeLength", out options.AdaptiveDLMinEdgeLength );
  183.             XmlHelper.GetValue( ref node, "AdaptiveDLThreshold", out options.AdaptiveDLThreshold );
  184.             XmlHelper.GetValue( ref node, "AdaptiveDLMaxSubdiv", out options.AdaptiveDLMaxSubdiv );
  185.             XmlHelper.GetValue( ref node, "IsAdaptiveBounce", out options.IsAdaptiveBounce );
  186.             XmlHelper.GetValue( ref node, "AdaptiveBounceMinEdgeLength", out options.AdaptiveBounceMinEdgeLength );
  187.             XmlHelper.GetValue( ref node, "AdaptiveBounceThreshold", out options.AdaptiveBounceThreshold );
  188.             XmlHelper.GetValue( ref node, "AdaptiveBounceMaxSubdiv", out options.AdaptiveBounceMaxSubdiv );
  189.             XmlHelper.GetValue( ref node, "OutputMesh", out options.OutputMesh );
  190.             XmlHelper.GetValue( ref node, "IsSaveCompressedResults", out options.IsSaveCompressedResults );
  191.             XmlHelper.GetValue( ref node, "IsBinaryOutputXFile", out options.IsBinaryOutputXFile );
  192.         }
  193.  
  194.         public void SaveOptions(string fileName)
  195.         {
  196.             if( fileName == String.Empty)
  197.                 fileName = this.fileName;
  198.  
  199.             XmlDocument doc = new XmlDocument();
  200.             XmlNode docNode = (XmlNode) doc;
  201.             XmlNode topNode;
  202.  
  203.             XmlHelper.CreateChildNode(ref doc, ref docNode, "PrtOptions", XmlNodeType.Element, out topNode);
  204.             XmlHelper.CreateNewValue( ref doc, ref topNode, "InitialDir", options.InitialDir );
  205.             XmlHelper.CreateNewValue( ref doc, ref topNode, "InputMesh", options.InputMesh );
  206.             XmlHelper.CreateNewValue( ref doc, ref topNode, "ResultsFile", options.ResultsFileName );
  207.             XmlHelper.CreateNewValue( ref doc, ref topNode, "Order", options.Order );
  208.             XmlHelper.CreateNewValue( ref doc, ref topNode, "NumberRays", options.NumberRays );
  209.             XmlHelper.CreateNewValue( ref doc, ref topNode, "NumberBounces", options.NumberBounces );
  210.             XmlHelper.CreateNewValue( ref doc, ref topNode, "ShowTooltips", options.ShowTooltips? 1 : 0 );
  211.  
  212.             XmlHelper.CreateNewValue( ref doc, ref topNode, "NumberClusters", options.NumberClusters );
  213.             XmlHelper.CreateNewValue( ref doc, ref topNode, "Quality", (int) options.Quality );
  214.             XmlHelper.CreateNewValue( ref doc, ref topNode, "NumberPcaVectors", options.NumberPcaVectors );
  215.  
  216.             XmlHelper.CreateNewValue( ref doc, ref topNode, "IsSubsurfaceScattering", options.IsSubsurfaceScattering? 1 : 0 );
  217.             XmlHelper.CreateNewValue( ref doc, ref topNode, "LengthScale", options.LengthScale );
  218.             XmlHelper.CreateNewValue( ref doc, ref topNode, "NumberChannels", (int)options.NumberChannels );
  219.             XmlHelper.CreateNewValue( ref doc, ref topNode, "PredefinedMatIndex", options.PredefinedMaterialIndex );
  220.             XmlHelper.CreateNewValue( ref doc, ref topNode, "Diffuse", options.Diffuse );
  221.             XmlHelper.CreateNewValue( ref doc, ref topNode, "Absorption", options.Absorption );
  222.             XmlHelper.CreateNewValue( ref doc, ref topNode, "ReducedScattering", options.ReducedScattering );
  223.             XmlHelper.CreateNewValue( ref doc, ref topNode, "RelativeIndexOfRefraction", options.RelativeIndexOfRefraction );
  224.  
  225.             XmlHelper.CreateNewValue( ref doc, ref topNode, "IsAdaptive", options.IsAdaptive? 1 : 0 );
  226.             XmlHelper.CreateNewValue( ref doc, ref topNode, "IsRobustMeshRefine", options.IsRobustMeshRefine? 1 : 0 );
  227.             XmlHelper.CreateNewValue( ref doc, ref topNode, "RobustMeshRefineMinEdgeLength", options.RobustMeshRefineMinEdgeLength );
  228.             XmlHelper.CreateNewValue( ref doc, ref topNode, "RobustMeshRefineMaxSubdiv", options.RobustMeshRefineMaxSubdiv );
  229.             XmlHelper.CreateNewValue( ref doc, ref topNode, "IsAdaptiveDL", options.IsAdaptiveDL? 1 : 0 );
  230.             XmlHelper.CreateNewValue( ref doc, ref topNode, "AdaptiveDLMinEdgeLength", options.AdaptiveDLMinEdgeLength );
  231.             XmlHelper.CreateNewValue( ref doc, ref topNode, "AdaptiveDLThreshold", options.AdaptiveDLThreshold );
  232.             XmlHelper.CreateNewValue( ref doc, ref topNode, "AdaptiveDLMaxSubdiv", options.AdaptiveDLMaxSubdiv );
  233.             XmlHelper.CreateNewValue( ref doc, ref topNode, "IsAdaptiveBounce", options.IsAdaptiveBounce? 1 : 0 );
  234.             XmlHelper.CreateNewValue( ref doc, ref topNode, "AdaptiveBounceMinEdgeLength", options.AdaptiveBounceMinEdgeLength );
  235.             XmlHelper.CreateNewValue( ref doc, ref topNode, "AdaptiveBounceThreshold", options.AdaptiveBounceThreshold );
  236.             XmlHelper.CreateNewValue( ref doc, ref topNode, "AdaptiveBounceMaxSubdiv", options.AdaptiveBounceMaxSubdiv );
  237.             XmlHelper.CreateNewValue( ref doc, ref topNode, "OutputMesh", options.OutputMesh );
  238.  
  239.             XmlHelper.CreateNewValue( ref doc, ref topNode, "IsSaveCompressedResults", options.IsSaveCompressedResults? 1 : 0 );
  240.             XmlHelper.CreateNewValue( ref doc, ref topNode, "IsBinaryOutputXFile", options.IsBinaryOutputXFile? 1 : 0 );
  241.  
  242.             doc.Save(fileName);
  243.         }
  244.  
  245.         public void ResetSettings()
  246.         {
  247.             options = new SimulatorOptions();
  248.  
  249.             options.InputMesh = "misc\\shapes1.x";
  250.             options.InitialDir = System.IO.Path.GetDirectoryName(Utility.FindMediaFile(options.InputMesh));
  251.             options.ResultsFileName = "shapes1_prtresults.pca";
  252.             options.Order = 6;
  253.             options.NumberRays = 1024;
  254.             options.NumberBounces = 1;
  255.             options.IsSubsurfaceScattering = false;
  256.             options.LengthScale = 25.0f;
  257.             options.NumberChannels = 3;
  258.  
  259.             options.PredefinedMaterialIndex = 0;
  260.             options.RelativeIndexOfRefraction = PrtOptions.PredefinedMaterials[0].RelativeIndexOfRefraction;
  261.             options.Diffuse = PrtOptions.PredefinedMaterials[0].Diffuse;
  262.             options.Absorption = PrtOptions.PredefinedMaterials[0].Absorption;
  263.             options.ReducedScattering = PrtOptions.PredefinedMaterials[0].ReducedScattering;
  264.  
  265.             options.IsAdaptive = false;
  266.             options.IsRobustMeshRefine = true;
  267.             options.RobustMeshRefineMinEdgeLength = 0.0f;
  268.             options.RobustMeshRefineMaxSubdiv = 2;
  269.             options.IsAdaptiveDL = true;
  270.             options.AdaptiveDLMinEdgeLength = 0.03f;
  271.             options.AdaptiveDLThreshold = 8e-5f;
  272.             options.AdaptiveDLMaxSubdiv = 3;
  273.             options.IsAdaptiveBounce = false;
  274.             options.AdaptiveBounceMinEdgeLength = 0.03f;
  275.             options.AdaptiveBounceThreshold = 8e-5f;
  276.             options.AdaptiveBounceMaxSubdiv = 3;
  277.             options.OutputMesh = "shapes1_adaptive.x";
  278.             options.IsBinaryOutputXFile = true;
  279.  
  280.             options.IsSaveCompressedResults = true;
  281.             //options.Quality = CompressionQuality.FastLowQuality;
  282.             options.Quality = CompressionQuality.SlowHighQuality;
  283.             options.NumberPcaVectors = 24;
  284.             options.NumberClusters = 1;
  285.         }
  286.     }
  287.  
  288.     class XmlHelper
  289.     {
  290.         public static void CreateChildNode( ref XmlDocument doc, ref XmlNode parentNode, string name, XmlNodeType type, out XmlNode newNode )
  291.         {
  292.             newNode = doc.CreateNode(type, name, null);
  293.             parentNode.AppendChild(newNode);
  294.         }
  295.  
  296.         public static void CreateNewValue( ref XmlDocument doc, ref XmlNode node, string name, string newValue )
  297.         {
  298.             XmlNode newNode;
  299.             XmlNode newTextNode;
  300.             CreateChildNode( ref doc, ref node, name, XmlNodeType.Element, out newNode );
  301.             CreateChildNode( ref doc, ref newNode, name, XmlNodeType.Text, out newTextNode );
  302.             newTextNode.Value = newValue;
  303.         }
  304.  
  305.         public static void CreateNewValue( ref XmlDocument doc, ref XmlNode node, string name, int newValue )
  306.         {
  307.             string stringValue = string.Format("{0}", newValue );
  308.             CreateNewValue( ref doc, ref node, name, stringValue);
  309.         }
  310.  
  311.         public static void CreateNewValue( ref XmlDocument doc, ref XmlNode node, string name, float newValue )
  312.         {
  313.             string stringValue = string.Format("{0}", newValue );
  314.             CreateNewValue( ref doc, ref node, name, stringValue);
  315.         }
  316.  
  317.         public static void CreateNewValue( ref XmlDocument doc, ref XmlNode node, string name, ColorValue newValue )
  318.         {
  319.             string stringValue = string.Format("{0}", newValue.ToArgb() );
  320.             CreateNewValue( ref doc, ref node, name, stringValue);
  321.         }
  322.  
  323.         public static void GetValue( ref XmlNode node, string name, out string value )
  324.         {
  325.             value = String.Empty;
  326.  
  327.             if( node == null )
  328.                 return;
  329.  
  330.             if(node.Name == name)
  331.             {
  332.                 if(node.FirstChild != null)
  333.                 {
  334.                     if(node.FirstChild.NodeType == XmlNodeType.Text)
  335.                         value = node.FirstChild.Value;
  336.                 }
  337.             }
  338.  
  339.             if(node.NextSibling != null)
  340.                 node = node.NextSibling;
  341.         }
  342.  
  343.         public static void GetValue( ref XmlNode node, string name, out int value )
  344.         {
  345.             string stringValue;
  346.             GetValue( ref node, name, out stringValue); if(stringValue == string.Empty) stringValue = "0";
  347.             value = int.Parse(stringValue);
  348.         }
  349.  
  350.         public static void GetValue( ref XmlNode node, string name, out bool value )
  351.         {
  352.             string stringValue;
  353.             GetValue( ref node, name, out stringValue);
  354.             if(stringValue == string.Empty)
  355.                 stringValue = "False";
  356.             else
  357.                 stringValue = "True";
  358.             value = bool.Parse(stringValue);
  359.         }
  360.  
  361.         public static void GetValue( ref XmlNode node, string name, out float value )
  362.         {
  363.             string stringValue;
  364.             GetValue( ref node, name, out stringValue);  if(stringValue == string.Empty) stringValue = "0";
  365.             value = float.Parse(stringValue);
  366.         }
  367.  
  368.         public static void GetValue( ref XmlNode node, string name, out ColorValue value )
  369.         {
  370.             string stringValue;
  371.             GetValue( ref node, name, out stringValue);  if(stringValue == string.Empty) stringValue = "0";
  372.             value = ColorValue.FromArgb(int.Parse(stringValue));
  373.         }
  374.     }
  375. }
  376.